
// Item plugins configs
#define MAX_ITEMS		100
#define MAX_ITEM_NAME_SIZE	64
#define MAX_ITEM_DESC_SIZE	128

//Admin Slots
#define ACCESS_LEVEL1 ADMIN_LEVEL_C
#define ACCESS_LEVEL2 ADMIN_LEVEL_D


new BoughtItemSound[] 	= { "items/ammopickup1.wav" }
new PickUpItemSound[] 	= { "items/ammopickup2.wav" }
new ItemModel[] 	= { "models/w_gaussammo.mdl" }
new JetpackModel[] 	= { "models/w_longjump.mdl" }


#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <engine>
#include <vault>
#include <fun>


new g_numberofitems
new g_menuPosition[33]
new g_itemindex[MAX_ITEMS+1]
new g_itemcost[MAX_ITEMS+1]
new g_itemname[MAX_ITEMS+1][MAX_ITEM_NAME_SIZE+1]
new g_itemdesc[MAX_ITEMS+1][MAX_ITEM_DESC_SIZE+1]
new bool:g_iPlayerItem[33][MAX_ITEMS+1]
new g_PlayerNumItems[33]


new PLUGIN_VERSION[] 	= "1.0"
new PLUGIN_NAME[] 	= "Shopmenu"
new PLUGIN_AUTHOR[] 	= "Liquidair"

public plugin_init() 
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
	register_cvar("sm_itemsremoved", "1")
	
	// Shopmenu Slot Access
	register_cvar( "max_playeritems", "3")

	
	register_clcmd("say /items", "open_ItemUpgrades")
	register_clcmd("say /itemlist", "display_itemlist")
	register_clcmd("say /help", 	"display_help")
	register_clcmd("say /myitems", 	"display_myitems")
	register_clcmd("say /dropitems","cmd_DropItems")
	register_concmd("items", 	"open_ItemUpgrades")
	register_concmd("itemlist", 	"display_itemlist")
	register_concmd("help", 	"display_help")
	register_concmd("myitems", 	"display_myitems")
	register_concmd("dropitems", 	"cmd_DropItems")
		
	register_touch("Item", "player", "Pickup_Items")
	
	register_event("DeathMsg", "Event_DeathMsg", "a")
	register_event("Damage", "Event_Damage", "bd", "2>0")
	//register_event("StatusValue", "Event_StatusValue", "bd", "1=2")

	register_event("TextMsg", "Event_GameRestart", "a", "2=#Game_will_restart_in", "2&#Game_C") 
	register_event("SendAudio", "Event_RoundEnd", "a", "2=%!MRAD_terwin", "2=%!MRAD_ctwin", "2=%!MRAD_rounddraw") 	

	register_menucmd(register_menuid("Item Shop"), 1023, "Action_ItemUpgrades")
}


public client_authorized(id)
{
	g_PlayerNumItems[id] = 0
	for(new i = 1; i <= MAX_ITEMS; ++i) {
		g_iPlayerItem[id][MAX_ITEMS] = false
	}
	
	if (access(id, ACCESS_LEVEL1)) {
		set_cvar_num("max_playeritems", 4)
	} 
	if (access(id, ACCESS_LEVEL2)) {
		set_cvar_num("max_playeritems", 5)
	}
}

public Event_RoundEnd()
{
	if(get_cvar_num("sm_itemsremoved")) {
		set_task(2.5, "remove_items")
	}
}

public remove_items()
{
	new items = find_ent_by_class(-1, "Item")
	while(items) 
	{
		remove_entity(items)
		items = find_ent_by_class(items, "Item")
	}
}

public Event_GameRestart()
{
	new Float:fRestartCvar = get_cvar_float("sv_restart")
	set_task((fRestartCvar - 0.2), "Event_RoundEnd")
}


public Event_StatusValue(id)
{
	new target = read_data(2)
  	if(target != id && target != 0)
  	{
		//new sName[32]
		new szMenuBody[512]
		//get_user_name(target, sName, 31)

		//new len = format(szMenuBody, 511, "Name: %s^n", sName)
		new len = format(szMenuBody[len], 511, "Items:")
		for(new i = 1; i <= g_numberofitems; ++i) if(g_iPlayerItem[target][i]) {
			len += format(szMenuBody[len], 511-len, "%s^n", g_itemname[i])
		}
		set_hudmessage(255, 255, 255, 0.2, 0.9, 2, 1.5, 10.0, 0.02, 5.0, 2)
		show_hudmessage(id, szMenuBody)
	}
}


public Event_DeathMsg() 
{ 
	new attacker = read_data(1)
	new victim = read_data(2)

	if(!is_user_connected(attacker) || !is_user_connected(victim)) {
		return PLUGIN_CONTINUE
	}
	
	Drop_Items(victim)

	if(get_user_team(attacker) == get_user_team(victim)) {
		return PLUGIN_CONTINUE
	}

	return PLUGIN_CONTINUE
}

public Event_Damage(id)
{
	if(!is_user_connected(id)) {
		return PLUGIN_CONTINUE
	}

	Drop_Items(id)

	return PLUGIN_CONTINUE
}

public display_myitems(id)
{
	new szMenuBody[512]
	new len = format(szMenuBody, 511, "Items: %d^n", g_PlayerNumItems[id])
	for(new i = 1; i <= g_numberofitems; ++i) if(g_iPlayerItem[id][i]) {
		len += format(szMenuBody[len], 511-len, "%s^n", g_itemname[i])
	}
	set_hudmessage(255, 255, 255, 0.2, 0.9, 2, 1.5, 10.0, 0.02, 5.0, 2)
	show_hudmessage(id, szMenuBody)
	
	return PLUGIN_HANDLED
}

public open_ItemUpgrades(id)
{
	display_ItemUpgrades(id, g_menuPosition[id] = 0)
	return PLUGIN_HANDLED
}

public display_help(id)
{
	new szMotd[2048], szTitle[64], iPos = 0
	format(szTitle, 63, "%s Version: %s", PLUGIN_NAME, PLUGIN_VERSION)
	iPos += format(szMotd[iPos], 2047-iPos, "<html><head><style type=^"text/css^">pre{color:#FFB000;}body{background:#000000;margin-left:8px;margin-top:0px;}</style></head><pre><body>")
	iPos += format(szMotd[iPos], 2047-iPos, "^n^n<b>%s</b>^n^n", szTitle)
	iPos += format(szMotd[iPos], 2047-iPos, "Client Commands:^n")
	iPos += format(szMotd[iPos], 2047-iPos, "say /items - Item shop menu^n")
	iPos += format(szMotd[iPos], 2047-iPos, "say /myitem - Displays your items^n")
	iPos += format(szMotd[iPos], 2047-iPos, "say /dropitems - Drop all your items^n")
	iPos += format(szMotd[iPos], 2047-iPos, "say /itemlist - Displays all items with description^n")
	iPos += format(szMotd[iPos], 2047-iPos, "say /help - Displays help^n")
	iPos += format(szMotd[iPos], 2047-iPos, "Can also type in the console without '/'^n")
	iPos += format(szMotd[iPos], 2047-iPos, "Bind example: bind home ummenu^n^n")
	
	show_motd(id, szMotd, szTitle)
	return PLUGIN_HANDLED
}

public display_itemlist(id)
{
	new szMotd[2048], szTitle[64], iPos = 0
	format(szTitle, 63, "Item List")
	iPos += format(szMotd[iPos], 2047-iPos, "<html><head><style type=^"text/css^">pre{color:#FFB000;}body{background:#000000;margin-left:8px;margin-top:0px;}</style></head><pre><body>")
	iPos += format(szMotd[iPos], 2047-iPos, "^n^n<b>%s</b>^n^n", szTitle)
	iPos += format(szMotd[iPos], 2047-iPos, "# Item Name | Item Description | Item Cost^n")
	
	for(new i = 1; i <= g_numberofitems; ++i) {
		iPos += format(szMotd[iPos], 2047-iPos, "(%d)%s   %s   %d^n", i, g_itemname[i], g_itemdesc[i], g_itemcost[i])
	}
	show_motd(id, szMotd, szTitle)
	return PLUGIN_HANDLED
}

public cmd_DropItems(id)
{
	if(!is_user_alive(id)) {
		return PLUGIN_HANDLED
	}
	
	if(g_PlayerNumItems[id] < 1) 
	{
		client_print(id, print_chat, "You dont have any items")
		return PLUGIN_HANDLED
	}
	Drop_Items(id)
	return PLUGIN_HANDLED
}

display_ItemUpgrades(id, pos)
{
	if(pos < 0) {
		return
	}
	
	new start = pos * 8
  	if(start >= g_numberofitems) {
    		start = pos = g_menuPosition[id]
	}

	new menuBody[512]
  	new len = format(menuBody, 511, "Item Shop^n^n")
	
  	new end = start + 8
  	new keys = MENU_KEY_0

	if(end > g_numberofitems) {
    		end = g_numberofitems
	}
	
	new b = 0
  	for(new a = start; a < end; ++a) 
	{
		new i = a + 1
		keys |= (1<<b)
		new money
		money = cs_get_user_money(id)
		if(g_iPlayerItem[id][i] ||(money < g_itemcost[i]) || g_PlayerNumItems[id] >= get_cvar_num("max_playeritems")) {
			len += format(menuBody[len], 511-len,"\r%d. %s (%d)\w^n", ++b, g_itemname[i], g_itemcost[i])
		} else {
			len += format(menuBody[len], 511-len,"%d. %s (%d)^n", ++b, g_itemname[i], g_itemcost[i])
		}
  	}

  	if(end != g_numberofitems)
	{
    		format(menuBody[len], 511-len, "^n9. %L...^n0. %L", id, "MORE", id, pos ? "BACK" : "EXIT")
    		keys |= MENU_KEY_9
  	}
  	else
	{
		format(menuBody[len], 511-len, "^n0. %L", id, pos ? "BACK" : "EXIT")
	}
  	show_menu(id, keys, menuBody, -1, "Item Shop")
}

public Action_ItemUpgrades(id, key)
{
	switch(key) 
	{
    		case 8: display_ItemUpgrades(id, ++g_menuPosition[id])
		case 9: display_ItemUpgrades(id, --g_menuPosition[id])
    		default:
		{
			++key
			new money
			new plugin_id = g_itemindex[g_menuPosition[id] * 8 + key]
			new item_id = g_menuPosition[id] * 8 + key
			new func = get_func_id("Enable_Item", plugin_id)
			money = cs_get_user_money(id)
			new cost = g_itemcost[item_id]
			if(g_PlayerNumItems[id] >= get_cvar_num("max_playeritems")) 
			{
				client_print(id, print_chat, "Only %d items", get_cvar_num("max_playeritems"))
				display_ItemUpgrades(id, g_menuPosition[id])
				return PLUGIN_HANDLED
			}
		
			if(g_iPlayerItem[id][item_id])
			{
				client_print(id, print_chat, "You already have %s", g_itemname[item_id])
				display_ItemUpgrades(id, g_menuPosition[id])
				return PLUGIN_HANDLED
			}
			
			if(money >= cost)
			{
				callfunc_begin_i(func, plugin_id)
				callfunc_push_int(id)
				callfunc_end()

				g_PlayerNumItems[id] += 1
				g_iPlayerItem[id][item_id] = true
				cs_set_user_money(id, money - cost, 1)
				client_cmd(id, "spk %s", BoughtItemSound)
				client_print(id, print_chat, "Item Bought: %s", g_itemname[item_id])
				client_print(id, print_chat, "Item Description: %s", g_itemdesc[item_id])
			
				if(g_PlayerNumItems[id] < get_cvar_num("max_playeritems")) {
					display_ItemUpgrades(id, g_menuPosition[id])
				}
			}
			else
			{
				client_print(id, print_chat, "You need $%d to buy %s", g_itemcost[item_id], g_itemname[item_id])
				display_ItemUpgrades(id, g_menuPosition[id])
			}
		}
	}
	return PLUGIN_HANDLED
}

public register_item(item_index, item_name[], item_desc[], item_cost)
{
	if(g_numberofitems == MAX_ITEMS) {
		return -2
	}
	
	g_numberofitems++
	g_itemindex[g_numberofitems] = item_index
	format(g_itemname[g_numberofitems], MAX_ITEM_NAME_SIZE, item_name)
	format(g_itemdesc[g_numberofitems], MAX_ITEM_DESC_SIZE, item_desc)
	g_itemcost[g_numberofitems] = item_cost
	
	return g_numberofitems
}

public Pickup_Items(ptr, ptd)
{
	if(is_user_alive(ptd) && is_valid_ent(ptr)) 
	{ 
		new item_id = entity_get_int(ptr, EV_INT_iuser4)
		new plugin_id = g_itemindex[item_id]
		new func = get_func_id("Enable_Item", plugin_id)
		if(!g_iPlayerItem[ptd][item_id])
		{
			if(g_PlayerNumItems[ptd] < get_cvar_num("max_playeritems"))
			{
				callfunc_begin_i(func, plugin_id)
				callfunc_push_int(ptd)
				callfunc_end()
						
				g_PlayerNumItems[ptd] += 1
				g_iPlayerItem[ptd][item_id] = true
				client_cmd(ptd, "spk %s", PickUpItemSound)
				client_print(ptd, print_chat, "Item Picked Up: %s", g_itemname[item_id])
					
				remove_entity(ptr)
			}
		}
	}
}

Drop_Items(id)
{
	for(new item_id = 1; item_id <= MAX_ITEMS ; item_id++) 
	{
		if(g_iPlayerItem[id][item_id])
		{
			new Float:fVelocity[3], Float:fOrigin[3], Float:fGlowColors[3] = {100.0, 100.0, 100.0}
			entity_get_vector(id, EV_VEC_origin, fOrigin)
			fOrigin[2] += 75
	
			VelocityByAim(id, 130, fVelocity)
			
			new item_ent = create_entity("info_target")
			if(is_valid_ent(item_ent))
			{
				entity_set_string(item_ent, EV_SZ_classname, "Item")
				
				entity_set_int(item_ent, EV_INT_solid, SOLID_TRIGGER)
				entity_set_int(item_ent, EV_ENT_owner, 0)
				entity_set_int(item_ent, EV_INT_movetype, 6)
				entity_set_int(item_ent, EV_INT_iuser4, item_id)
				entity_set_int(item_ent, EV_INT_renderfx, kRenderFxGlowShell)

				entity_set_vector(item_ent, EV_VEC_origin, fOrigin)
				entity_set_vector(item_ent, EV_VEC_velocity, fVelocity)
				entity_set_vector(item_ent, EV_VEC_rendercolor, fGlowColors)
				
				if(g_itemname[item_id] = "Jetpack") {
					entity_set_model(item_ent, JetpackModel)
				} else {
					entity_set_model(item_ent, ItemModel)
				}
				entity_set_size(item_ent, Float:{-2.5, -2.5, -1.5}, Float:{2.5, 2.5, 1.5})
				
				new plugin_id = g_itemindex[item_id]
				new func = get_func_id("Disable_Item", plugin_id)

				callfunc_begin_i(func, plugin_id)
				callfunc_push_int(id)
				callfunc_end()
				
				g_iPlayerItem[id][item_id] = false
				g_PlayerNumItems[id] = 0
			}
		}
	}
	return PLUGIN_CONTINUE
}

public plugin_precache() 
{
	precache_model(ItemModel)
	precache_model(JetpackModel)
	precache_sound(PickUpItemSound)
	precache_sound(BoughtItemSound)
}

